
package com.swak.frame.exception;

import java.util.Collection;
import java.util.Map;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import com.swak.frame.dto.base.RestApiCode;
import com.swak.frame.dto.base.SysRestCode;

/**
 * ClassName: Assert.java 
 * @Description: ApiAssert
 * @author colley.ma
 * @date 2021年3月15日
 */
public class ApiAssert {
	public static void isTrue(boolean expression, String message) {
		if (!expression) {
			ApiExceptionUtil.throwException(SysRestCode.INVALID_PARAM, message);
		}
	}

	public static void isTrue(boolean expression) {
		isTrue(expression, "[ApiAssert failed] - this expression must be true");
	}

	public static void isTrue(boolean expression, RestApiCode restApiCode) {
		if (!expression) {
			ApiExceptionUtil.throwException(restApiCode);
		}
	}

	public static void isNull(Object object, String message) {
		if (object != null) {
			ApiExceptionUtil.throwException(SysRestCode.INVALID_PARAM, message);
		}
	}

	public static void isNull(Object object) {
		isNull(object, "[ApiAssert failed] - the object argument must be null");
	}

	public static void notNull(Object object, String message) {
		if (object == null) {
			ApiExceptionUtil.throwException(SysRestCode.INVALID_PARAM, message);
		}
	}

	public static void notNull(Object object, RestApiCode restApiCode) {
		if (object == null) {
			ApiExceptionUtil.throwException(restApiCode);
		}
	}

	public static void notNull(Object object) {
		notNull(object, "[ApiAssert failed] - this argument is required; it must not be null");
	}

	public static void notEmpty(Object[] array, String message) {
		if (ArrayUtils.isEmpty(array)) {
			ApiExceptionUtil.throwException(SysRestCode.INVALID_PARAM, message);
		}
	}

	public static void notEmpty(Object[] array) {
		notEmpty(array, "[ApiAssert failed] - this array must not be empty: it must contain at least 1 element");
	}

	public static void noNullElements(Object[] array, String message) {
		if (array != null) {
			for (int i = 0; i < array.length; i++) {
				if (array[i] == null) {
					ApiExceptionUtil.throwException(SysRestCode.INVALID_PARAM, message);
				}
			}
		}
	}

	public static void noNullElements(Object[] array) {
		noNullElements(array, "[ApiAssert failed] - this array must not contain any null elements");
	}

	public static void notEmpty(Collection<?> collection, String message) {
		if (CollectionUtils.isEmpty(collection)) {
			ApiExceptionUtil.throwException(SysRestCode.INVALID_PARAM, message);
		}
	}

	public static void notEmpty(Collection<?> collection) {
		notEmpty(collection,
				"[ApiAssert failed] - this collection must not be empty: it must contain at least 1 element");
	}

	@SuppressWarnings("rawtypes")
	public static void notEmpty(Map map, String message) {
		if (MapUtils.isEmpty(map)) {
			ApiExceptionUtil.throwException(SysRestCode.INVALID_PARAM, message);
		}
	}

	public static void notEmpty(Map<?,?> map) {
		notEmpty(map, "[ApiAssert failed] - this map must not be empty; it must contain at least one entry");
	}

	public static void notEmpty(String strval, String message) {
		if (StringUtils.isEmpty(strval)) {
			ApiExceptionUtil.throwException(SysRestCode.INVALID_PARAM, message);
		}
	}

	public static void notEmpty(String strval, RestApiCode restApiCode) {
		if (StringUtils.isEmpty(strval)) {
			ApiExceptionUtil.throwException(restApiCode);
		}
	}

	public static void notEmpty(String strval) {
		notEmpty(strval,
				"[ApiAssert failed] - this String value must not be empty; it must contain at least one entry");
	}

	@SuppressWarnings("rawtypes")
	public static void isInstanceOf(Class clazz, Object obj) {
		isInstanceOf(clazz, obj, "");
	}

	@SuppressWarnings("rawtypes") 
	public static void isInstanceOf(Class type, Object obj, String message) {
		notNull(type, "Type to check against must not be null");

		if (!type.isInstance(obj)) {
			throw new ApiException(SysRestCode.INVALID_PARAM, message + ". Object of class ["
					+ ((obj != null) ? obj.getClass().getName() : "null") + "] must be an instance of " + type);
		}
	}

	@SuppressWarnings("rawtypes") 
	public static void isAssignable(Class superType,Class subType) {
		isAssignable(superType, subType, "");
	}

	@SuppressWarnings({ "rawtypes", "unchecked" }) 
	public static void isAssignable(Class superType,
			 Class subType, String message) {
		notNull(superType, "Type to check against must not be null");
		if ((subType == null) || !superType.isAssignableFrom(subType)) {
			throw new ApiException(SysRestCode.INVALID_PARAM,
					message + subType + " is not assignable to " + superType);
		}
	}

	public static void state(boolean expression, String message) {
		if (!expression) {
			ApiExceptionUtil.throwException(SysRestCode.INVALID_PARAM, message);
		}
	}

	public static void state(boolean expression) {
		state(expression, "[ApiAssert failed] - this state invariant must be true");
	}
}
